﻿
Partial Public Class GestorReports
    Inherits ChildWindow

    Public Sub New()
        InitializeComponent()
        OkButton.IsEnabled = False

        'afegim tots els controls de configuració de reports (que hereten de ConfitReportAbstract)
        For Each report As TipusReport In ConfigReportAbstract.ReportList
            Select Case Contexte.UsuariActual.IdTipusUsuari
                Case EnumTipusUsuari.Administrador
                    addReport(report.ConfigReport)
                Case EnumTipusUsuari.Manager
                    Dim vReport As TipusReport = report
                    If (From UR As Web.UsuariReport In Contexte.UsuariActual.UsuariReports Where UR.IdReport = vReport.TipusReport).Count > 0 Then
                        addReport(report.ConfigReport)
                    End If
                Case Else
                    'No fem res.
            End Select
        Next

    End Sub

    Private configs As New Dictionary(Of String, ConfigReportAbstract)
    Private Sub addReport(config As ConfigReportAbstract)
        listReports.Items.Add(config.ToString)
        configs.Add(config.ToString, config)
    End Sub

    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim config As ConfigReportAbstract = Nothing
        If configs.TryGetValue(listReports.SelectedItem, config) Then
            config.Generar()
            'Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.DialogResult = True
    End Sub

    Private Sub Scroll_SizeChanged(sender As System.Object, e As System.Windows.SizeChangedEventArgs)
        Me.Components.MaxHeight = Math.Max(Me.Scroll.ViewportHeight, Me.Components.MinHeight)
        Me.Components.MaxWidth = Math.Max(Me.Scroll.ViewportWidth, Me.Components.MinWidth)
    End Sub

    Private Sub listReports_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
        gridDetall.Children.Clear()
        If e.AddedItems.Count = 0 Then
            Me.gridDetall.Visibility = Windows.Visibility.Collapsed
            OkButton.IsEnabled = False
        Else
            Me.gridDetall.Visibility = Windows.Visibility.Visible
            Dim config As ConfigReportAbstract = Nothing
            If configs.TryGetValue(e.AddedItems(0), config) Then
                Me.gridDetall.Children.Add(config)
                OkButton.IsEnabled = True
            End If
        End If
    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        Diagnostics.Debug.WriteLine("Destruim el GestorReports")
    End Sub

End Class
